package com.gitee.zycra.future.job.client.executor;

import com.gitee.zycra.future.job.client.container.SystemInfoContainer;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;

import java.math.BigDecimal;

/**
 * @author zycra
 */
public final class CPUPercentExecutor implements Runnable {

    private long[] prevTicks;

    private static final CentralProcessor PROCESSOR = new SystemInfo().getHardware().getProcessor();

    public CPUPercentExecutor() {
        this.prevTicks = PROCESSOR.getSystemCpuLoadTicks();
    }

    @Override
    public void run() {
        SystemInfoContainer.CPU_USED_RATIO.set(BigDecimal.valueOf(PROCESSOR.getSystemCpuLoadBetweenTicks(prevTicks))
                .multiply(SystemInfoContainer.HUNDRED).intValue());
        this.prevTicks = PROCESSOR.getSystemCpuLoadTicks();
    }
}
